package com.nuoniu.sibanyun.service.project.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.project.DevProject;
import com.nuoniu.sibanyun.entity.project.RdTools;
import com.nuoniu.sibanyun.entity.project.SysRdtForm;
import com.nuoniu.sibanyun.mapper.project.DevProjectMapper;
import com.nuoniu.sibanyun.service.project.IDevProjectService;
import com.nuoniu.sibanyun.service.project.IRdToolsService;
import com.nuoniu.sibanyun.service.project.ISysRdtFormService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 白强
 * @since 2021-08-31
 */
@Service
public class DevProjectServiceImpl extends ServiceImpl<DevProjectMapper, DevProject> implements IDevProjectService {

    @Autowired
    private IRdToolsService rdToolsService;
    @Autowired
    private ISysRdtFormService sysRdtFormService;

    @Override
    public Page<DevProject> pageList(Page page,DevProject devProject) {
        QueryWrapper<DevProject> wrapper = new QueryWrapper<>();
        page = page(page, wrapper);
        return page;
    }

    @Override
    public void saveDevProject(DevProject devProject) {
        UserInfo userInfo = UserThreadLocal.get();
        boolean checkName = checkName(devProject.getName(), devProject.getId());
        if(checkName) {
            throw new NuoNiuException("项目名称已经存在");
        }
        boolean checkCode = checkCode(devProject.getCode(), devProject.getId());
        if(checkCode) {
            throw new NuoNiuException("项目编码已经存在");
        }
        if( null == devProject.getId()){
            devProject.setCreateDate(LocalDateTime.now());
            devProject.setCreateUserId(userInfo.getUserId());
            devProject.setCreateUserName(userInfo.getRealName());
        }
        saveOrUpdate(devProject);
    }

    @Override
    public void delete(Integer id) {
        if ( null == id) {
            throw new NuoNiuException("请选择项目");
        }
        boolean used = checkProjectUsed(id);
        if(used) {
            throw new NuoNiuException("该项目已经使用，不能删除");
        }
        DevProject devProject = getById(id);
        if (null == devProject) {
            throw new NuoNiuException("项目不存在");
        }
        removeById(id);
    }

    @Override
    public boolean checkName(String projectName, Long id) {
        if (StringUtils.isBlank(projectName)) {
            throw new NuoNiuException("项目名称不能为空");
        }
        boolean b = baseMapper.checkName(projectName, id);
        return b;
    }

    @Override
    public boolean checkCode(String projectCode, Long id) {
        if (StringUtils.isBlank(projectCode)) {
            throw new NuoNiuException("项目编码不能为空");
        }
        boolean b = baseMapper.checkCode(projectCode, id);
        return b;
    }

    @Override
    public boolean checkProjectUsed(Integer id) {
        QueryWrapper<SysRdtForm> sysRdtFormQueryWrapper = new QueryWrapper<>();
        sysRdtFormQueryWrapper.eq("project_id",id);
        int sysRdtFormCount = sysRdtFormService.count(sysRdtFormQueryWrapper);
        if(sysRdtFormCount > 0){
            return true;
        }
        QueryWrapper<RdTools> rdToolsQueryWrapper = new QueryWrapper<>();
        rdToolsQueryWrapper.eq("project_id",id);
        int count = rdToolsService.count(rdToolsQueryWrapper);
        if(count > 0){
            return true;
        }
        return false;
    }

}
